<template>
  <Dialog v-model="dialogVisible" title="修改" width="60%">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" v-loading="formLoading"
      :disabled="formDisabled">
      <el-form-item label="上级目录" prop="parentId">
        <el-tree-select v-model="formData.parentId" :data="treeList" :props="defaultProps" check-strictly node-key="id"
          placeholder="请选择上级目录" />

        <!-- <el-input v-model="formData.parentId" placeholder="请输入上级目录" /> -->
      </el-form-item>
      <el-form-item label="名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileClass">
        <el-select v-model="formData.fileClass" placeholder="请选择文件类型"
          :disabled="(formData.isFolder == 1 || formData.fileClass == 5) ? true : false">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.OA_FILE_CLASS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="文档内容" prop="fileContent" v-if="formData.fileClass == 5">
        <Editor v-model="formData.fileContent" :readonly="formDisabled" ref="editorRef" />
      </el-form-item>
    </el-form>
    <template #footer v-if="!formDisabled">
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as FileApi from '@/api/infra/file'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { defaultProps, handleTree } from '@/utils/tree'
import { ElTree } from 'element-plus'
import { Editor } from '@/components/Editor'
import { fi } from 'element-plus/es/locale'

defineOptions({ name: 'UpdateFileForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formDisabled = ref(false) // 是否禁用表单
const formData = ref({
  id: undefined,
  title: '',
  isFolder: 1,
  parentId: undefined as number | any,
  fileClass: undefined as number | any,
  fileContent: '',
})

const formRules = reactive({
  title: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (id: number | any, type: string, disabled?: boolean) => {
  resetForm()
  getTree()
  dialogVisible.value = true
  formDisabled.value = disabled || false // 是否禁用表单
  // 表示在线文档
  if (type == 'online') {
    formData.value.fileClass = 5
    formData.value.isFolder = 0
  }
  if (id) {
    const res = await FileApi.getDetail(id)
    formData.value = res
    if (res.parentId == 0) {
      formData.value.parentId = undefined
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 提交表单 */
const submitForm = () => {
  // 校验表单
  formRef.value.validate()
  formLoading.value = true

  console.log(formData.value)
  if (formData.value.parentId == undefined) {
    formData.value.parentId = 0
  }

  if (formData.value.id) {
    FileApi.updateDetail(formData.value)
    message.success(t('common.updateSuccess'))
  } else {
    FileApi.createFile(formData.value)
    message.success(t('common.createSuccess'))
  }

  dialogVisible.value = false
  formLoading.value = false
  // 发送操作成功的事件
  emit('success')
}

/** 重置表单 */
const resetForm = () => {
  // 重置上传状态和文件
  formLoading.value = false
  formData.value = {
    id: undefined,
    title: '',
    isFolder: 1,
    parentId: undefined,
    fileClass: undefined,
    fileContent: '',
  }
  formRef.value?.resetFields()
}

const treeList = ref<Tree[]>([]) // 树形结构
const treeRef = ref<InstanceType<typeof ElTree>>()

/** 获得目录树 */
const getTree = async () => {
  const res = await FileApi.getAllTree()
  treeList.value = []
  treeList.value.push(...handleTree(res))
}

/** 初始化 **/
onMounted(() => {
  getTree()
})
</script>